package com._520it.crm.web.controller;

import com._520it.crm.domain.Client;
import com._520it.crm.domain.Employee;
import com._520it.crm.domain.TransferRecord;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.TransferRecordQueryObject;
import com._520it.crm.service.IClientService;
import com._520it.crm.service.IEmployeeService;
import com._520it.crm.service.ITransferRecordService;
import com._520it.crm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/transferRecord")
public class TransferRecordController {
    @Autowired
    private ITransferRecordService transferRecordService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("")
    public String index(){
        return "transferRecord";
    }


    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(TransferRecordQueryObject qo)
    {
        return transferRecordService.query(qo);
    }

    @RequestMapping("/transfer")
    @ResponseBody
    public AjaxResult transfer(@RequestParam("id")Long clientId, TransferRecord transferRecord)
    {
        try {
            transferRecordService.insert(transferRecord ,clientId);
            Client client = clientService.selectByPrimaryKey(clientId);
            Employee employee = employeeService.selectByPrimaryKey(client.getPrinciUser());
            return new AjaxResult(true,"移交成功!"+client.getRealName()+"将由"+employee.getName()+"负责");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("移交失败!");
        }

    }

    @RequestMapping("/removeAll")
    @ResponseBody
    public AjaxResult removeAll()
    {
        try {
            transferRecordService.deleteall();
            return new AjaxResult(true,"清除所有记录成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("清除所有记录失败!");
        }

    }
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Integer id)
    {
        try {
            transferRecordService.deleteByPrimaryKey(id);
            return new AjaxResult(true,"删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("失败失败!");
        }

    }
}
